package com.welove.util;

import java.util.Hashtable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.widget.Adapter;
import android.widget.ListView;

public class StringUtil {

	// 判断是否为邮箱
	public int checkEmail(String str) {
		if (!TextUtils.isEmpty(str)) {
			String regex = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
			Pattern pattern = Pattern.compile(regex);
			Matcher matcher = pattern.matcher(str);
			if (matcher.matches()) {
				return 1;
			}
		} else {
			return -1;
		}
		return 0;
	}

	// 判断手机号
	public int checkPhone(String str) {
		if (!TextUtils.isEmpty(str)) {
			String regex = "^0?(13[0-9]|15[012356789]|18[0236789]|14[57])[0-9]{8}$";
			Pattern pattern = Pattern.compile(regex);
			Matcher matcher = pattern.matcher(str);
			if (matcher.matches()) {
				return 1;
			}
		} else {
			return -1;
		}

		return 0;
	}

	// 判断是否为字符串长度
	public int checkLength(String str, int min, int max) {
		if (!TextUtils.isEmpty(str)) {

			if (str.length() > max || str.length() < min) {

				return 0;
			} else {

				return 1;
			}

		} else {
			return -1;
		}

	}

	// 解析Struts返回的结果
	public String parseStr(String str) {
		return str.replaceAll("[\\p{Punct}\\p{Space}]+", "");
	}

	// 根据经纬度计算距离
	private static double EARTH_RADIUS = 6378137.0;

	public static double GetDistance(double lat1, double lng1, double lat2,
			double lng2) {
		double radLat1 = rad(lat1);
		double radLat2 = rad(lat2);
		double a = radLat1 - radLat2;
		double b = rad(lng1) - rad(lng2);
		double distance = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
				+ Math.cos(radLat1) * Math.cos(radLat2)
				* Math.pow(Math.sin(b / 2), 2)));
		distance = distance * EARTH_RADIUS;
		distance = Math.round(distance * 10000) / 10000;
		// System.out.println(distance);
		return distance;
	}

	private static double rad(double d) {
		return d * Math.PI / 180.0000;
	}

	public static int dip2px(Context context, float dipValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dipValue * scale + 0.5f);
	}

	public static int px2dip(Context context, float pxValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}

	public boolean isFullScreen(Activity mContext, ListView lv,
			Adapter adapter, int dip) {

		int screenHeight = mContext.getWindowManager().getDefaultDisplay()
				.getHeight()
				- dip2px(mContext, dip);
		int totalHeight = 0;
		int count = adapter.getCount();

		for (int i = 0; i < count; i++) {

			View listItem = adapter.getView(i, null, lv);

			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();

		}

		int height = totalHeight + (lv.getDividerHeight() * (count - 1));

		if (height < screenHeight) {
			return false;
		}

		return true;

	}
	
	public static int plToLevel(String plStr)
	{
		
		int pl=Integer.parseInt(plStr)/2;
		
		int level=pl%100;
		
		return level;
	}

}
